package com.hotel.wallet.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.common.config.RabbitKeyConfig;
import com.hotel.common.param.GrowthAdd;
import com.hotel.common.param.MqMsg;
import com.hotel.common.vo.R;
import com.hotel.wallet.config.RabbitConfig;
import com.hotel.wallet.mapper.HotelRoomMapper;
import com.hotel.wallet.mapper.HotelRoomOrderMapper;
import com.hotel.user.entity.*;
import com.hotel.wallet.mapper.PayLogMapper;
import com.hotel.wallet.mapper.UserMoneyLogMapper;
import com.hotel.wallet.mapper.UserWalletMapper;
import com.hotel.wallet.service.PayLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 支付宝交易日志表 服务实现类
 * </p>
 *
 * @author zhy
 * @since 2024-06-17
 */
@Service
public class PayLogServiceImpl extends ServiceImpl<PayLogMapper, PayLog> implements PayLogService {

    @Autowired
    private PayLogMapper payLogMapper;

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Autowired
    private UserMoneyLogMapper userMoneyLogMapper;

    @Autowired
    private HotelRoomOrderMapper hotelRoomOrderMapper;

    @Autowired
    private HotelRoomMapper hotelRoomMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public R alipayCallback(String no) {

        // 1.修改订单支付状态
        QueryWrapper<PayLog> payLogQueryWrapper = new QueryWrapper<>();
        payLogQueryWrapper.eq("no",no);

        PayLog payLog = payLogMapper.selectOne(payLogQueryWrapper);
        if (payLog == null) {
            return R.fail("支付失败");
        }
        payLog.setStatus(2);
        payLogMapper.updateById(payLog);
//        if (num > 0) {
//            return R.ok("修改订单支付状态成功");
//        }

        if (no.startsWith("z")) {

            // 2.账户余额增加数据
            QueryWrapper<UserWallet> userWalletQueryWrapper = new QueryWrapper<>();
            userWalletQueryWrapper.eq("user_id",payLog.getUserId());

            UserWallet userWallet = userWalletMapper.selectOne(userWalletQueryWrapper);

            if (userWallet == null) {
                return R.fail("支付失败");
            }
            // 更新余额和账户总额(总充值额度)
            // wallet中的账户余额等于余额＋充值额度
            userWallet.setMoney(userWallet.getMoney() + payLog.getMoney());
            userWallet.setTotalMoney(userWallet.getTotalMoney() + payLog.getMoney());

            int res = userWalletMapper.updateById(userWallet);
            if (res <= 0) {
                return R.fail("支付失败");
            }

            // 3.账户记录表记录日志
            UserMoneyLog userMoneyLog = new UserMoneyLog();
            userMoneyLog.setUserId(payLog.getUserId())// 用户表id
                    .setMoney(payLog.getMoney()/100)// 酒店钱包余额变动
                    .setType(1)// 类型 1增加 2减少
                    .setFlag("充值")// 变化的原因 充值 签到 等级等级
                    .setInfo("支付宝充值")// 描述
                    .setCtime(new Date());// 创建时间

            // 插入日志
            userMoneyLogMapper.insert(userMoneyLog);

            // 4.返回充值成功
            return R.ok("充值成功");
        } else if (no.startsWith("v")) {

        } else if (no.startsWith("r")) {
            // 1.更新订单状态
            QueryWrapper<HotelRoomOrder> hotelRoomOrderQueryWrapper = new QueryWrapper<>();
            hotelRoomOrderQueryWrapper.eq("room_no",no);
            HotelRoomOrder hotelRoomOrder = hotelRoomOrderMapper.selectOne(hotelRoomOrderQueryWrapper);
            if (hotelRoomOrder == null) {
                return R.fail("未找到订单");
            }

            hotelRoomOrder.setStatus(2);
            hotelRoomOrderMapper.updateById(hotelRoomOrder);

            // 2.修改房间状态
            QueryWrapper<HotelRoom> roomQueryWrapper = new QueryWrapper<>();
            roomQueryWrapper.eq("id",hotelRoomOrder.getRoomId());
            HotelRoom hotelRoom = hotelRoomMapper.selectOne(roomQueryWrapper);
            if (hotelRoom.getStatus() == 1) {
                hotelRoom.setStatus(2);
                hotelRoom.setUtime(new Date());
                hotelRoomMapper.updateById(hotelRoom);
            } else if (hotelRoom.getStatus() == 2) {
                hotelRoom.setUtime(new Date());
                hotelRoomMapper.updateById(hotelRoom);
            } else {
                return R.fail("充值失败");
            }

            // 3.向消息队列发送增加积分消息
            rabbitTemplate.convertAndSend(
                    "",
                    RabbitKeyConfig.Q_USER_GROWTH,
                    new MqMsg(IdUtil.getSnowflakeNextId(),
                            4,
                            new GrowthAdd(payLog.getUserId(), "预约房间", 500)));

            return R.ok("充值成功");

        }
        return R.fail("充值失败");
    }
}
